<template>
  <div class="login_container">
    <div class="login_box">
      <!-- logo 头像 -->
      <div class="avatar_box">
        <img src="../../assets/logo.png" alt="logo" class="avatar">
      </div>
      <!-- 表单 -->
      <el-form
        ref="loginFormRef"
        class="boxForm"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-s-custom"
          />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-suoding"
            type="password"
          />
        </el-form-item>
        <!-- 登录/重置-->
        <el-form-item class="buttom_box">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" plain @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { setItem } from '@/utils/storage';
import { login } from '@/api/login.js';
export default {
  name: 'Login',
  components: {},
  data() {
    return {
      /* 表单 */
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      /* 表单验证 */
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    /* 重置 */
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    /* 登录 */
    login() {
      this.$refs.loginFormRef.validate(async(valid) => {
        if (!valid) return;
        try {
          const { data: res } = await login(this.loginForm);
          if (res.meta.status === 200) {
            this.$message.success('恭喜你，登录成功！');
            /*
              1、将登录成功之后的token,保存到客户端的sessionStorage 中
               1.1项目中除了登录之外的其他api接口，必须登录之后才能访问
               1.2 token 只能在当前网站打开期间生效，所以，保存在sessionStorage中
          */
            setItem('token', res.data.token);

            // 2.通过编程式导航跳转到后台主页，路由地址是/home
            this.$router.push('/home');
          } else {
            this.$message.error('登录失败！');
          }
        } catch (error) {
          console.log(error, 'error');
        }
      });
    }
  }
};
</script>
<style  lang='scss' scoped>
.login_container {
  height: 100%;
  background: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      padding: 10px;
      border-radius: 50%;
      box-shadow: 0px 0px 10px 0px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      .avatar {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .boxForm {
      padding: 0 20px;
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      width: 100%;
      .buttom_box {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
